package com.tsd.jxc.vo;

import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;

import java.math.BigDecimal;

/**
 * 进销存的设置模型
 */
public class JxcSettingVo {
    public static final String JXC_SETTING_CODE = "jxc_setting";
    //id，sid，code存放 SysSettingExt 对象对应的属性
    private Long id;
    private String sid;
    private String code;
    //启用值
    public static final int STATE_ENABLE = 1;
    //关闭值
    public static final int STATE_DISABLE = 0;

    // 采购单自动审核
    private int auto_approval_buy_order = STATE_DISABLE;
    // 采购退货单自动审核
    private int auto_approval_buy_return = STATE_DISABLE;
    // 销售报价自动审核
    private int auto_approval_sale_quotation = STATE_DISABLE;
    // 销售单自动审核
    private int auto_approval_sale_order = STATE_DISABLE;
    // 销售退货单自动审核
    private int auto_approval_sale_return = STATE_DISABLE;
    // 入库单自动审核
    private int auto_approval_in_bill = STATE_DISABLE;
    // 出库单自动审核
    private int auto_approval_out_bill = STATE_DISABLE;
    // 盘点单自动审核
    private int auto_approval_stock_inventory = STATE_DISABLE;
    // 开票申请自动审核
    private int auto_approval_sale_invoice = STATE_DISABLE;
    // 收票申请自动审核
    private int auto_approval_buy_invoice = STATE_DISABLE;
    // 进项税率
    private BigDecimal buy_tax_ratio = BigDecimal.valueOf(0.13D);
    // 服务税率
    private BigDecimal service_tax_ratio = BigDecimal.valueOf(0.09D);
    // 附加税率
    private BigDecimal ext_tax_ratio = BigDecimal.valueOf(0.12D);
    // 采购订单只有一个供应商
    private int buy_order_only_one_supplier = STATE_DISABLE;
    // 销售订单审核控制毛利率
    private int sale_order_audit_check_gross_profit_rate = STATE_DISABLE;
    // 销售订单审核控制毛利率
    private BigDecimal default_gross_profit_rate = BigDecimal.ZERO;
    // 销售订单审核控制毛利率
    private BigDecimal default_tax_ratio = BigDecimal.ZERO;
    // 自动生成收票申请
    private int auto_generate_buy_invoice = STATE_DISABLE;
    // 自动确认收票
    private int auto_confirm_buy_invoice = STATE_DISABLE;
    // 行业配置：普通、服装
    public static final String INDUSTRY_CONFIG_COMMON = "common";
    public static final String INDUSTRY_CONFIG_CLOTHING = "clothing";
    private String industry_config = INDUSTRY_CONFIG_COMMON;
    // 服装尺寸预设
    private String clothing_preset_size = "XS,S,M,L,XL,2XL,3XL;28,29,30,31,32,33,34";
    // 单价小数位数
    private int price_fraction = 2;
    // 金额小数位数
    private int amount_fraction = 2;

    public static final String RECONCILIATION_SOURCE_ORDER = "order";
    public static final String RECONCILIATION_SOURCE_SEND = "send";
    public static final String RECONCILIATION_SOURCE_STOCK_BILL = "stockBill";
    private String reconciliation_source = RECONCILIATION_SOURCE_ORDER;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getAuto_approval_buy_order() {
        return auto_approval_buy_order;
    }

    public void setAuto_approval_buy_order(int auto_approval_buy_order) {
        this.auto_approval_buy_order = auto_approval_buy_order;
    }

    public int getAuto_approval_buy_return() {
        return auto_approval_buy_return;
    }

    public void setAuto_approval_buy_return(int auto_approval_buy_return) {
        this.auto_approval_buy_return = auto_approval_buy_return;
    }

    public int getAuto_approval_sale_quotation() {
        return auto_approval_sale_quotation;
    }

    public void setAuto_approval_sale_quotation(int auto_approval_sale_quotation) {
        this.auto_approval_sale_quotation = auto_approval_sale_quotation;
    }

    public int getAuto_approval_sale_order() {
        return auto_approval_sale_order;
    }

    public void setAuto_approval_sale_order(int auto_approval_sale_order) {
        this.auto_approval_sale_order = auto_approval_sale_order;
    }

    public int getAuto_approval_sale_return() {
        return auto_approval_sale_return;
    }

    public void setAuto_approval_sale_return(int auto_approval_sale_return) {
        this.auto_approval_sale_return = auto_approval_sale_return;
    }

    public int getAuto_approval_in_bill() {
        return auto_approval_in_bill;
    }

    public void setAuto_approval_in_bill(int auto_approval_in_bill) {
        this.auto_approval_in_bill = auto_approval_in_bill;
    }

    public int getAuto_approval_out_bill() {
        return auto_approval_out_bill;
    }

    public void setAuto_approval_out_bill(int auto_approval_out_bill) {
        this.auto_approval_out_bill = auto_approval_out_bill;
    }

    public int getAuto_approval_stock_inventory() {
        return auto_approval_stock_inventory;
    }

    public void setAuto_approval_stock_inventory(int auto_approval_stock_inventory) {
        this.auto_approval_stock_inventory = auto_approval_stock_inventory;
    }

    public int getAuto_approval_sale_invoice() {
        return auto_approval_sale_invoice;
    }

    public void setAuto_approval_sale_invoice(int auto_approval_sale_invoice) {
        this.auto_approval_sale_invoice = auto_approval_sale_invoice;
    }

    public int getAuto_approval_buy_invoice() {
        return auto_approval_buy_invoice;
    }

    public void setAuto_approval_buy_invoice(int auto_approval_buy_invoice) {
        this.auto_approval_buy_invoice = auto_approval_buy_invoice;
    }

    public BigDecimal getBuy_tax_ratio() {
        return buy_tax_ratio;
    }

    public void setBuy_tax_ratio(BigDecimal buy_tax_ratio) {
        this.buy_tax_ratio = buy_tax_ratio;
    }

    public BigDecimal getService_tax_ratio() {
        return service_tax_ratio;
    }

    public void setService_tax_ratio(BigDecimal service_tax_ratio) {
        this.service_tax_ratio = service_tax_ratio;
    }

    public BigDecimal getExt_tax_ratio() {
        return ext_tax_ratio;
    }

    public void setExt_tax_ratio(BigDecimal ext_tax_ratio) {
        this.ext_tax_ratio = ext_tax_ratio;
    }

    public int getBuy_order_only_one_supplier() {
        return buy_order_only_one_supplier;
    }

    public void setBuy_order_only_one_supplier(int buy_order_only_one_supplier) {
        this.buy_order_only_one_supplier = buy_order_only_one_supplier;
    }

    public int getSale_order_audit_check_gross_profit_rate() {
        return sale_order_audit_check_gross_profit_rate;
    }

    public void setSale_order_audit_check_gross_profit_rate(int sale_order_audit_check_gross_profit_rate) {
        this.sale_order_audit_check_gross_profit_rate = sale_order_audit_check_gross_profit_rate;
    }

    public BigDecimal getDefault_gross_profit_rate() {
        return default_gross_profit_rate;
    }

    public void setDefault_gross_profit_rate(BigDecimal default_gross_profit_rate) {
        this.default_gross_profit_rate = default_gross_profit_rate;
    }

    public BigDecimal getDefault_tax_ratio() {
        return default_tax_ratio;
    }

    public void setDefault_tax_ratio(BigDecimal default_tax_ratio) {
        this.default_tax_ratio = default_tax_ratio;
    }


    public int getAuto_generate_buy_invoice() {
        return auto_generate_buy_invoice;
    }

    public void setAuto_generate_buy_invoice(int auto_generate_buy_invoice) {
        this.auto_generate_buy_invoice = auto_generate_buy_invoice;
    }

    public int getAuto_confirm_buy_invoice() {
        return auto_confirm_buy_invoice;
    }

    public void setAuto_confirm_buy_invoice(int auto_confirm_buy_invoice) {
        this.auto_confirm_buy_invoice = auto_confirm_buy_invoice;
    }

    public String getIndustry_config() {
        return industry_config;
    }

    public void setIndustry_config(String industry_config) {
        this.industry_config = industry_config;
    }

    public String getClothing_preset_size() {
        return clothing_preset_size;
    }

    public void setClothing_preset_size(String clothing_preset_size) {
        this.clothing_preset_size = clothing_preset_size;
    }

    public int getPrice_fraction() {
        return price_fraction;
    }

    public void setPrice_fraction(int price_fraction) {
        this.price_fraction = price_fraction;
    }

    public int getAmount_fraction() {
        return amount_fraction;
    }

    public void setAmount_fraction(int amount_fraction) {
        this.amount_fraction = amount_fraction;
    }

    public String getReconciliation_source() {
        return reconciliation_source;
    }

    public void setReconciliation_source(String reconciliation_source) {
        this.reconciliation_source = reconciliation_source;
    }

    public boolean isCheck_gross_profit_rate() {
        if (STATE_ENABLE == this.getSale_order_audit_check_gross_profit_rate()) {
            return !HlpUtils.isEmpty(this.getDefault_gross_profit_rate()) && this.getDefault_gross_profit_rate().doubleValue() > 0;
        }
        return false;
    }

    public boolean isClothing() {
        return INDUSTRY_CONFIG_CLOTHING.equals(this.getIndustry_config());
    }

    /**
     * 计算税额
     * @param amount 金额
     * @param ratio 税率
     * @param amountWithTax 金额是否含税
     * @return
     */
    public BigDecimal getTaxAmount(BigDecimal amount, BigDecimal ratio, boolean amountWithTax) {
        if (amountWithTax) {
            // 税额 = 金额 / (1 + 税率) * 税率
            double taxAmount = amount.doubleValue();

            double addRatio = DoubleUtil.add(1D, ratio);
            taxAmount = DoubleUtil.div(taxAmount, addRatio, 4);

            taxAmount = DoubleUtil.mul(taxAmount, ratio);

            taxAmount = DoubleUtil.round(taxAmount, 2);
            return BigDecimal.valueOf(taxAmount);
        }
        return BigDecimal.valueOf(DoubleUtil.round(DoubleUtil.mul(amount, ratio), 2));
    }
}
